package com.lottery.im.module.contact.ui;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.chat.ui.ChatActivity;
import com.lottery.im.module.contact.adapter.GroupListAdapter;
import com.lottery.im.module.tim.event.GroupEvent;
import com.lottery.im.module.tim.model.GroupInfo;
import com.lottery.im.module.tim.model.GroupProfile;
import com.lottery.im.module.tim.model.SectionGroupProfile;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/*
 * 我的群组
 * */
public class GroupListFragment extends BaseSwipeFragment implements Observer {

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.tv_empty) TextView mTvEmpty;
  @BindView(R.id.list) RecyclerView mList;

  private List<SectionGroupProfile> mSectionGroupProfiles;
  private GroupListAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_group_list;
  }

  @Override protected void initPresenter() {
    initView();
  }

  private void initView() {
    //注册群关系监听
    GroupEvent.getInstance().addObserver(this);
    mTvTitle.setText("我的群聊");
    mSectionGroupProfiles = new ArrayList<>();
    mList.setLayoutManager(new LinearLayoutManager(getActivity()));
    RecyclerViewDivider.with(mActivity)
        .color(ContextCompat.getColor(mActivity, R.color.windowBackground))
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mList);
    mAdapter = new GroupListAdapter(mSectionGroupProfiles);
    mAdapter.setOnItemClickListener((adapter, view, position) -> {

      GroupProfile groupProfile = mSectionGroupProfiles.get(position).t;
      if (null != groupProfile) {
        ChatActivity.navToChat(mActivity, groupProfile.getIdentify(), TIMConversationType.Group);
      }
    });
    mList.setAdapter(mAdapter);
    setUI();
  }

  private void setUI() {
    mSectionGroupProfiles.clear();
    // 我创建的群聊+我加入的群聊分组
    List<GroupProfile> selfGroupProfiles = new ArrayList<>();
    List<GroupProfile> joinGroupProfiles = new ArrayList<>();

    List<GroupProfile> list = GroupInfo.getInstance().getGroupListByType(GroupInfo.publicGroup);
    for (GroupProfile summary : list) {
      if (summary.getRole() == TIMGroupMemberRoleType.Owner) {
        selfGroupProfiles.add(summary);
      } else {
        joinGroupProfiles.add(summary);
      }
    }

    if (selfGroupProfiles.size() > 0) {
      SectionGroupProfile sectionGroupProfile = new SectionGroupProfile(true, "我创建的群");
      mSectionGroupProfiles.add(sectionGroupProfile);
      for (GroupProfile profile : selfGroupProfiles) {
        SectionGroupProfile sectionProfile = new SectionGroupProfile(profile);
        mSectionGroupProfiles.add(sectionProfile);
      }
    }

    if (joinGroupProfiles.size() > 0) {
      SectionGroupProfile sectionGroupProfile = new SectionGroupProfile(true, "我加入的群");
      mSectionGroupProfiles.add(sectionGroupProfile);
      for (GroupProfile profile : joinGroupProfiles) {
        SectionGroupProfile sectionProfile = new SectionGroupProfile(profile);
        mSectionGroupProfiles.add(sectionProfile);
      }
    }

    if (mSectionGroupProfiles.size() > 0) {
      mAdapter.notifyDataSetChanged();
      mTvEmpty.setVisibility(View.GONE);
      mList.setVisibility(View.VISIBLE);
    } else {
      mTvEmpty.setVisibility(View.VISIBLE);
      mList.setVisibility(View.GONE);
    }
  }

  @OnClick(R.id.iv_back) public void onViewClicked() {
    popBackStack();
  }

  @Override public void update(Observable observable, Object data) {
    if (observable instanceof GroupEvent) {
      new Handler(Looper.getMainLooper()).postDelayed(() -> setUI(), 1000);
    }
  }

  @Override public void onDestroy() {
    super.onDestroy();
    GroupEvent.getInstance().deleteObserver(this);
  }
}
